package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.RoleDTO;
import com.xzq.erp.domain.dto.RolePermissionDTO;
import com.xzq.erp.domain.po.SysPermission;
import com.xzq.erp.domain.query.RolePageQuery;
import com.xzq.erp.domain.vo.RoleVO;
import com.xzq.erp.service.ISysRoleService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-19
 */
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
@Tag(name = "角色相关接口")
public class SysRoleController {

    private final ISysRoleService service;

    /**
     * 分页查询角色列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询角色列表")
    @ApiOperationLog(description = "分页查询角色列表")
    @PreAuthorize("hasAuthority('role:list') || hasRole('ADMIN')")
    public Response<PageDTO<RoleVO>> getRoleList(@ParameterObject @Validated RolePageQuery query) {
        return Response.success(service.getRoleList(query));
    }

    /**
     * 根据角色ID查询权限
     */
    @GetMapping("/permissions/{roleId}")
    @Operation(summary = "根据角色ID查询权限的ID")
    @ApiOperationLog(description = "根据角色ID查询权限的ID")
    @PreAuthorize("hasAuthority('role:permissions') || hasRole('ADMIN')")
    public Response<List<Integer>> getRolePermissions(@Parameter(description = "角色ID") @PathVariable Integer roleId) {
        return Response.success(service.getRolePermissions(roleId));
    }

    /**
     * 添加角色
     */
    @PostMapping
    @Operation(summary = "添加角色")
    @ApiOperationLog(description = "添加角色")
    @PreAuthorize("hasAuthority('role:add') || hasRole('ADMIN')")
    public Response<Void> addRole(@RequestBody @Validated RoleDTO roleDTO) {
        service.addRole(roleDTO);
        return Response.success();
    }

    /**
     * 修改角色
     */
    @PutMapping
    @Operation(summary = "修改角色")
    @ApiOperationLog(description = "修改角色")
    @PreAuthorize("hasAuthority('role:update') || hasRole('ADMIN')")
    public Response<Void> updateRole(@RequestBody @Validated RoleDTO roleDTO) {
        service.updateRole(roleDTO);
        return Response.success();
    }

    /**
     * 修改角色状态
     */
    @PutMapping("/{roleId}/{status}")
    @Operation(summary = "修改角色状态")
    @ApiOperationLog(description = "修改角色状态")
    @PreAuthorize("hasAuthority('role:update') || hasRole('ADMIN')")
    public Response<Void> updateRoleStatus(@Parameter(description = "角色ID") @PathVariable Integer roleId,
                                           @Parameter(description = "角色状态") @PathVariable Integer status) {
        service.updateRoleStatus(roleId, status);
        return Response.success();
    }

    /**
     * 分配权限
     */
    @PutMapping("/permissions")
    @Operation(summary = "分配权限")
    @ApiOperationLog(description = "分配权限")
    @PreAuthorize("hasAuthority('role:permissions') || hasRole('ADMIN')")
    public Response<Void> assignPermissions(@RequestBody @Validated RolePermissionDTO permissionDTO) {
        service.assignPermissions(permissionDTO);
        return Response.success();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    @ApiOperationLog(description = "删除角色")
    @PreAuthorize("hasAuthority('role:delete') || hasRole('ADMIN')")
    public Response<Void> deleteRole(@Parameter(description = "角色ID") @PathVariable Integer id) {
        service.deleteRole(id);
        return Response.success();
    }
}
